package design

import (
	"gpt-api/framework/cache"
	"gpt-api/framework/cfg"
	"gpt-api/framework/db"
	"gpt-api/pkg/basicdata"
	"sync"
	"time"
)

type singleTon struct {
	db    *db.DB
	cache *cache.RedisClient
	loc   *time.Location
	basic *basicdata.BasicData
}

func (s *singleTon) GetDB() *db.DB {
	return s.db
}

func (s *singleTon) GetCache() *cache.RedisClient {
	return s.cache
}

func (s *singleTon) GetLocation() *time.Location {
	return s.loc
}

func (s *singleTon) GetBasic() *basicdata.BasicData {
	return s.basic
}

var (
	once   sync.Once
	single *singleTon
)

func GetSingleInstance() *singleTon {
	once.Do(func() {
		redisCli := cfg.NewRedis("redis")
		fdb := cfg.NewPostgreSQL("postgres")
		loc, _ := time.LoadLocation("UTC")
		basic := basicdata.New()
		basic.Start()

		single = &singleTon{
			cache: redisCli,
			db:    fdb,
			loc:   loc,
			basic: basic,
		}
	})
	return single
}
